<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="user" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addFn(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="Editor(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delFn(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <AddDept v-if="showDialog" :code-list="codeList" :name-list="namecop" :is-editor="isEditor" :show-dialog="showDialog" :zpid="pid" @close-dialog="handleClose" @sxym="sxym" @Cance="showDialog=false" />
    </div>
  </div>
</template>

<script>
import AddDept from './components/add-dept'
import { tranListToTreeData } from '@/utils/index'
import { getDepartments, delDepartment } from '../../api/departmenrs'
export default {
  components: {
    AddDept
  },
  data() {
    return {
      // 判断是添加还是编辑
      isEditor: false,
      // 传入子组件的id值
      pid: null,
      // 弹出层
      showDialog: false,
      // 把员工数据渲染到页面上数组
      list: [],
      codeList: [],
      originList: []
    }
  },
  computed: {
    // 通过计算属性把数组里面的数据子级的筛选出来再把子级里面的name取出来
    namecop() {
      if (this.isEditor) {
        // 编辑时的判断传值
        // 通过查找获取点击的父节点
        const curNode = this.originList.find(item => item.pid === this.pid)
        // 通过过滤器找到点击父节点的每一个子节点通过map获取到他的name值
        const name = this.originList.filter(item => item.pid === curNode.pid).map(item => item.name)
        // 通过return返回
        return name
      } else {
        // 登录时的判断传值
        // 获取点击的父节点下的所有子节点并获取他的name值
        const namedl = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
        console.log(namedl)
        return namedl
      }
    }
  },

  // 创建后获取数据铺设页面
  created() {
    this.DepartmentsApi()
  },
  methods: {
    // 获取所有员工信息
    async DepartmentsApi() {
      const res = await getDepartments()
      const list = tranListToTreeData(res.data.data.depts)
      // 部门编码获取全部数据的编码数据
      this.codeList = res.data.data.depts.map(item => item.code)
      // 部门名字获取所有部门的名字和id还有子部门的pid
      this.originList = res.data.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name
        }
      })
      this.list = list
    },
    // 删除员工信息
    delFn(id) {
      this.$confirm('确定删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartment(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.DepartmentsApi()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加员工信息
    addFn(id) {
      this.showDialog = true
      this.pid = id
      this.isEditor = false
    },
    // 点击弹出层右上角x关闭弹出层
    handleClose(bool) {
      this.showDialog = bool
    },
    // 新增完关闭弹出层
    sxym() {
      this.DepartmentsApi()
    },
    // 编辑按钮
    Editor(id) {
      // 点击编辑把判断登录和编辑的变量改为true
      this.isEditor = true
      // 点击编辑弹出弹出层
      this.showDialog = true
      // 把id给到编辑后台接口
      this.pid = id
    }
  }
}
</script>

<style>

</style>
